/**
 * 挂号费模版
 */
/**
 * registration/save 挂号费保持
 * registration/update 挂号费更改
 * registration/delete 挂号费删除
 * registration/list 挂号费列表
 * registration/one/:id 查找
 */

const express = require('express');
const router = express.Router();
// 引入操作的模型 
const RegistrationModel = require('../db/RegistrationModel');
const DepartmentModel = require('../db/DepartmentModel');
const PatientModel = require('../db/PatientModel');//患者模型
const UserModel = require('../db/UserModel');//用户模型
const RoleModel = require('../db/RoleModel');

router.post('/hadregister', function (req, res, next) {
    RegistrationModel.findOne({ doctorid: req.body.doctorid, department: req.body.department }).then((data) => {
        if (data) {
            return res.json({ code: 201, mes: '该医生的该科室挂号费已经设置！' });
        } else {
            return res.json({ code: 200, mes: '该名称可用！' });
        }
    }).catch((err) => {
        console.log(err);
        return res.json({ code: 201, mes: '发生意外错误！' })
    });
});


// POST /save 模版 
router.post('/save', function (req, res, next) {
    // 获取条件
    if (!req.body.doctorid || !req.body.doctorname || !req.body.department || !req.body.level || !req.body.price) {
        return res.json({ code: 201, mes: '缺少必填信息！' });
    }

    const registration = new RegistrationModel(req.body);
    registration.save().then(() => {
        return res.json({ code: 200, mes: '模版创建成功' })
    }).catch((err) => {
        console.log(err);
        return res.json({ code: 201, mes: '发生意外错误！' })
    })
});
// 更新角色
router.post('/update', function (req, res, next) {
    // 更改密码的判断需要额外处理
    const set = req.body;
    const id = req.body.id;
    RegistrationModel.findById(id).then(
        (data) => {
            if (data == null) { return res.json({ code: 201, mes: '传入id错误' }); }
            else {
                return RegistrationModel.update({ _id: id }, { $set: set }).then(
                    () => { return res.json({ code: 200, mes: '更改成功' }); }
                ).catch(() => {
                    return res.json({ code: 201, mes: '更改失败！' })
                });
            }
        }
    ).catch(function () {
        return res.json({ code: 201, mes: '发生意外错误！' });
    });
});

// 挂号列表模版
router.get('/list', function (req, res, next) {
    let departments = [];
    return DepartmentModel.find({}, ['name']).lean(true).then(names => {
        departments = names;
        return RegistrationModel.find().lean(true).then((registrations) => {
            let temp = {};
            for (let i = 0; i < departments.length; i++) {
                temp[departments[i]._id] = departments[i];
            }
            for (let i = 0; i < registrations.length; i++) {
                registrations[i].department = temp[registrations[i].department];
            }
            return registrations
        })
    }).then(registrations => {
        return Promise.all(registrations.map(values => {
            return UserModel.find({ _id: values.doctorid }, { role: 1, name: 1 }).then(userInfo => {
                values.role = userInfo[0].role;
                values.doctorname = userInfo[0].name;
                return values
            })
        }))
    }).then(registrations => {
        return Promise.all(registrations.map(values => {
            return RoleModel.find({ _id: values.role }).then(userInfo => {
                values.role = userInfo[0].name
                return values
            })
        }))
    }).then(registrations => {
        let temp = [];
        for (let i = 0; i < registrations.length; i++) {
            if (registrations[i].role == "医生") {
                temp.push(registrations[i])
            }
        }
        return res.json({ code: 200, data: temp });
    }).catch((err) => {
        return res.json({ code: 201, mes: '发生意外错误！', err });
    });
});

// 查找指定挂号模版
router.get('/one/:id', function (req, res, next) {
    const id = req.params.id;
    RegistrationModel.findById(id).then((registration) => {
        return res.json({ code: 200, registration });
    }).catch(() => {
        return res.json({ code: 201, mes: '发生意外错误！' });
    });
});
// 删除挂号模版
router.post('/delete', function (req, res, next) {
    const id = req.body.id;
    RegistrationModel.remove({ _id: id }).then(() => {
        return res.json({ code: 200, mes: '删除成功！' });
    }).catch(() => {
        return res.json({ code: 201, mes: '发生意外错误！' });
    });
});


module.exports = router;
